package com.chapter15generics.section3.generics;

import java.util.Iterator;

public class IterableFibonacci2 implements Iterable<Integer>{

    private int n = 0;

    private Fibonacci fibonacci;

    public IterableFibonacci2(int count,Fibonacci fibonacci){
        this.n = count;
        this.fibonacci = fibonacci;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            @Override
            public boolean hasNext() {
                return n-- > 0;
            }

            @Override
            public Integer next() {
                return fibonacci.next();
            }
        };
    }

    public static void main(String[] args) {
        IterableFibonacci2 iterableFibonacci2 = new IterableFibonacci2(5,new Fibonacci());
        for(int i : iterableFibonacci2){
            System.out.println(i + " ");
        }
    }
}
